var projectService = require('../Service/projectService.js');
var taskService = require('../Service/taskService.js');
var enums = require('../common/enum.js');

exports.list = function (req, res) {
    var projectId = req.params.ProjectId;
    projectService.get(projectId, function (project) {
        res.render('task', {Project: project});
    });
};

exports.getTags = function (req, res) {
    taskService.getTags(function (err, data) {
        res.json(data);
    });
};

exports.create = function (req, res) {
    var task = {};
    task.ProjectId = req.param('ProjectId');
    task.TaskName = req.param('TaskName');
    task.DeadLine = new Date(req.param('DeadLine'));
    task.Priority = req.param('Priority');
    task.Status = req.param('Status');
    task.Description = req.param('Description');
    task.AssignTo = req.param('AssignTo');
    task.Creator = req.session.User.UserId;
    task.CreateDate = new Date();
    var tags = [];
    var tagList = req.param('Tags');

    for (var i = 0; i < tagList.length; i++) {
        if (tagList[i].Checked) {
            tags.push(tagList[i].TagId);
        }
    }
    taskService.create(task, tags, function () {
        res.json({Result: "OK"});
    });
};

exports.query = function (req, res) {
    var condition = {};
    condition.ProjectId = req.param('ProjectId');
    condition.Keyword = req.param('Keyword');
    condition.AssignTo = req.param('AssignTo');
    condition.PageNo = req.param('PageNo');
    condition.PageSize = req.param('PageSize');

    var StatusList = [];
    if (req.param('New')) {
        StatusList.push(enums.TaskStatus.New);
    }
    if (req.param('InProgress')) {
        StatusList.push(enums.TaskStatus.InProgress);
    }
    if (req.param('Completed')) {
        StatusList.push(enums.TaskStatus.Completed);
    }
    if (req.param('Canceled')) {
        StatusList.push(enums.TaskStatus.Canceled);
    }
    condition.StatusList = StatusList;

    var PriorityList = [];
    if (req.param('Low')) {
        PriorityList.push(enums.TaskPriority.Low);
    }
    if (req.param('Normal')) {
        PriorityList.push(enums.TaskPriority.Normal);
    }
    if (req.param('High')) {
        PriorityList.push(enums.TaskPriority.High);
    }
    condition.PriorityList = PriorityList;

    var TagList = [];
    if (req.param('TagId') != null) {
        TagList.push(req.param('TagId'));
    }
    condition.TagList = TagList;

    taskService.query(condition, function (taskList, rowCount) {
        res.json({TaskList: taskList, RowCount: rowCount});
    });
};

exports.update = function (req, res) {
    var task = {};
    task.TaskId = req.param('TaskId');
    task.TaskName = req.param('TaskName');
    task.DeadLine = new Date(req.param('DeadLine'));
    task.Priority = req.param('Priority');
    task.Status = req.param('Status');
    task.Description = req.param('Description');
    task.AssignTo = req.param('AssignTo');

    taskService.update(task, function (result) {
        res.json({Result: result});
    });
};

exports.detail = function (req, res) {
    var taskId = req.params.TaskId;
    var projectId = req.params.ProjectId;
    projectService.get(projectId, function (project) {
        res.render('taskdetail', {Project: project, TaskId: taskId});
    });
};

exports.get = function (req, res) {
    var taskId = req.param('TaskId');
    taskService.get(taskId, function (task) {
        res.json({Task: task});
    });
};